#!/bin/bash

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
WHITE='\033[1;37m'
NC='\033[0m' # 无颜色

# 安装路径
INSTALL_BASE_DIR="$HOME/Napcat"
QQ_BASE_PATH="$INSTALL_BASE_DIR/opt/QQ"
TARGET_FOLDER="$QQ_BASE_PATH/resources/app/app_launcher"
QQ_EXECUTABLE="$QQ_BASE_PATH/qq"

# 风云插件路径
FENGYUN_PLUGIN_PATH="/root/XRK-Yunzai/plugins/fengyun-plugin"
FENGYUN_INSTALLED=false
INSTALL_FENGYUN_ONLY=false
INSTALL_QQ_NAPCAT=true
INSTALL_FYNT=true

# 全局变量
system_arch=""
package_manager=""

# 美观的标题
function show_header() {
    clear
    echo -e "${PURPLE}"
    echo "╔══════════════════════════════════════════════════╗"
    echo "║           风云 NapCat QQ 安装脚本                ║"
    echo "║                终极增强版                        ║"
    echo "╚══════════════════════════════════════════════════╝"
    echo -e "${NC}"
    echo ""
    echo -e "${CYAN}⭐ 进程状态显示 ⭐ 智能依赖修复 ⭐ 多线程下载${NC}"
    echo ""
}

# 分区标题
function show_section() {
    local title="$1"
    echo -e "${BLUE}╔══════════════════════════════════════════════════╗${NC}"
    echo -e "${BLUE}║${CYAN}          $title${BLUE}                    ║${NC}"
    echo -e "${BLUE}╚══════════════════════════════════════════════════╝${NC}"
    echo ""
}

# 带样式的日志
function log() {
    local time=$(date +"%H:%M:%S")
    local message="$1"
    
    case "$message" in
        *"失败"*|*"错误"*) echo -e "${RED}✗ [${time}] $message${NC}" ;;
        *"成功"*) echo -e "${GREEN}✓ [${time}] $message${NC}" ;;
        *"警告"*) echo -e "${YELLOW}⚠ [${time}] $message${NC}" ;;
        *) echo -e "${WHITE}» [${time}] $message${NC}" ;;
    esac
}

# 显示进程状态（新增）
function show_process_status() {
    local message="$1"
    local pid="$2"
    
    if [ -n "$pid" ]; then
        echo -e "${CYAN}↻ [$(date +"%H:%M:%S")] $message (PID: $pid)${NC}"
    else
        echo -e "${CYAN}↻ [$(date +"%H:%M:%S")] $message${NC}"
    fi
}

# 检查系统权限
function check_sudo() {
    if ! command -v sudo &>/dev/null; then
        log "错误: 请先安装sudo"
        exit 1
    fi
}

# 获取系统架构
function get_system_arch() {
    arch=$(uname -m)
    case "$arch" in
        x86_64) system_arch="amd64" ;;
        aarch64|arm64) system_arch="arm64" ;;
        *) system_arch="amd64" ;;
    esac
    log "系统架构: $system_arch"
}

# 检测包管理器
function detect_package_manager() {
    if command -v apt-get &>/dev/null; then
        package_manager="apt-get"
    elif command -v dnf &>/dev/null; then
        package_manager="dnf"
    elif command -v yum &>/dev/null; then
        package_manager="yum"
    else
        log "错误: 不支持的系统"
        exit 1
    fi
    log "包管理器: $package_manager"
}

# 等待其他包管理器进程完成（优化版，显示进程信息）
function wait_for_package_manager() {
    log "检查系统包管理器状态..."
    
    # 检查是否有apt/dpkg进程在运行
    local apt_processes=$(ps aux | grep -E 'apt|dpkg' | grep -v grep | grep -v "$0")
    
    if [ -n "$apt_processes" ]; then
        echo -e "${YELLOW}══════════════════════════════════════════════════${NC}"
        echo -e "${YELLOW}发现以下包管理器进程正在运行:${NC}"
        echo ""
        echo "$apt_processes" | while read line; do
            echo -e "  ${CYAN}$line${NC}"
        done
        echo ""
        
        local max_wait=180  # 最长等待3分钟
        local wait_time=0
        local check_interval=5
        
        echo -e "${YELLOW}等待现有安装进程完成...${NC}"
        echo -e "${CYAN}(按 Ctrl+C 跳过等待，但可能导致安装失败)${NC}"
        echo ""
        
        while pgrep -f "apt|dpkg" > /dev/null 2>&1 && [ $wait_time -lt $max_wait ]; do
            # 显示倒计时和当前进程
            local remaining=$((max_wait - wait_time))
            local current_process=$(ps aux | grep -E 'apt|dpkg' | grep -v grep | grep -v "$0" | head -1 | awk '{print $11 " (PID:" $2 ")"}')
            
            if [ -n "$current_process" ]; then
                echo -ne "${CYAN}等待中... ${remaining}秒 | 当前进程: $current_process${NC}\r"
            else
                echo -ne "${CYAN}等待中... ${remaining}秒${NC}\r"
            fi
            
            sleep $check_interval
            ((wait_time+=check_interval))
            
            # 每30秒显示一次详细状态
            if [ $((wait_time % 30)) -eq 0 ]; then
                echo ""
                log "仍在等待包管理器进程完成..."
            fi
        done
        
        echo ""  # 换行
        
        if [ $wait_time -ge $max_wait ]; then
            log "警告：等待超时，可能存在僵死的进程"
            echo -e "${YELLOW}尝试继续安装，如果遇到锁错误可能需要手动处理${NC}"
            return 1
        else
            log "包管理器进程已完成，继续安装"
            return 0
        fi
    else
        log "没有检测到其他包管理器进程，继续安装"
        return 0
    fi
}

# 配置APT多线程下载（优化版，显示下载进程）
function configure_apt_multithread() {
    if [ "$package_manager" != "apt-get" ]; then
        return
    fi
    
    show_process_status "检查下载加速工具..."
    
    if ! command -v aria2c &>/dev/null; then
        show_process_status "安装aria2多线程下载工具..."
        sudo apt-get install -y aria2 >/dev/null 2>&1 &
        local aria2_pid=$!
        
        # 显示安装进度
        while ps -p $aria2_pid > /dev/null 2>&1; do
            echo -ne "${CYAN}安装aria2... 请稍候${NC}\r"
            sleep 2
        done
        echo -ne "${GREEN}安装aria2... 完成${NC}\n"
    fi
    
    if command -v aria2c &>/dev/null; then
        show_process_status "配置APT多线程下载..."
        
        # 创建APT配置文件使用aria2
        sudo tee /etc/apt/apt.conf.d/80-aria2 > /dev/null << 'EOF'
Acquire {
  Queue-Mode "access";
  Retries "3";
  MaxParallelDownloads "8";
  Timeout "120";
};
Acquire::http::User-Agent "Debian APT-HTTP/1.3";
Acquire::http::Pipeline-Depth "5";
Acquire::http::No-Cache "1";
Acquire::http::Timeout "60";
Acquire::https::Timeout "60";
EOF
        
        # 设置aria2为下载器
        echo 'Acquire::http::Downloader "aria2c";' | sudo tee -a /etc/apt/apt.conf.d/80-aria2 > /dev/null
        echo 'Acquire::https::Downloader "aria2c";' | sudo tee -a /etc/apt/apt.conf.d/80-aria2 > /dev/null
        echo 'Acquire::ftp::Downloader "aria2c";' | sudo tee -a /etc/apt/apt.conf.d/80-aria2 > /dev/null
        
        log "✓ APT多线程下载配置完成"
    fi
}

# 安装依赖（优化版，显示安装进程）
function install_dependency() {
    show_section "安装系统依赖"
    
    # 等待其他包管理器进程完成
    wait_for_package_manager
    
    detect_package_manager
    
    # 配置多线程下载
    configure_apt_multithread
    
    show_process_status "更新软件包列表..."
    if [ "$package_manager" = "apt-get" ]; then
        # 显示更新进度
        sudo apt-get update -y 2>&1 | while read line; do
            if [[ $line == Get:* ]] || [[ $line == Hit:* ]] || [[ $line == Ign:* ]]; then
                echo -ne "${CYAN}更新软件源... $line${NC}\r"
            fi
        done
        echo -ne "${GREEN}更新软件源... 完成${NC}\n"
        
        show_process_status "安装基础工具 (curl, wget, unzip, jq, xvfb)..."
        local install_pid=$(sudo apt-get install -y curl wget unzip jq xvfb 2>&1 >/dev/null & echo $!)
        
        # 显示安装进度
        while ps -p $install_pid > /dev/null 2>&1; do
            echo -ne "${CYAN}安装基础工具... 进行中${NC}\r"
            sleep 1
        done
        echo -ne "${GREEN}安装基础工具... 完成${NC}\n"
        
        log "✓ 基础依赖安装完成"
    elif [ "$package_manager" = "dnf" ] || [ "$package_manager" = "yum" ]; then
        sudo $package_manager install -y curl wget unzip jq xorg-x11-server-Xvfb 2>&1 | while read line; do
            if [[ $line == Downloading* ]] || [[ $line == Installing* ]]; then
                echo -ne "${CYAN}安装基础工具... $line${NC}\r"
            fi
        done
        echo -ne "${GREEN}安装基础工具... 完成${NC}\n"
        log "✓ 基础依赖安装完成"
    fi
}

# 检查和安装缺失的依赖库
function check_and_install_deps() {
    local qq_path="$QQ_EXECUTABLE"
    
    if [ ! -f "$qq_path" ]; then
        log "QQ可执行文件不存在，跳过依赖检查"
        return
    fi
    
    show_process_status "检查QQ运行所需的系统库..."
    
    # 使用ldd检查缺失的库
    local missing_libs=$(ldd "$qq_path" 2>/dev/null | grep "not found" | awk '{print $1}')
    
    if [ -z "$missing_libs" ]; then
        log "✓ 所有系统库依赖已满足"
        return 0
    fi
    
    echo -e "${YELLOW}发现缺失的库: $missing_libs${NC}"
    show_process_status "安装缺失的系统依赖库..."
    
    # 包管理器特定的安装逻辑
    case "$package_manager" in
        "apt-get")
            _install_deps_apt "$missing_libs"
            ;;
        "dnf"|"yum")
            _install_deps_rpm "$missing_libs"
            ;;
        *)
            log "不支持的包管理器，无法自动安装依赖"
            echo -e "${RED}请手动安装以下库文件对应的软件包:${NC}"
            echo "$missing_libs"
            return 1
            ;;
    esac
    
    # 再次检查是否还有缺失
    show_process_status "验证依赖安装结果..."
    local still_missing=$(ldd "$qq_path" 2>/dev/null | grep "not found")
    if [ -n "$still_missing" ]; then
        log "警告: 部分依赖可能未成功安装"
        return 1
    else
        log "✓ 系统依赖库安装完成"
        return 0
    fi
}

# APT专用安装函数
function _install_deps_apt() {
    local missing_libs="$1"
    local packages_to_install=""
    
    # 库文件到包名的映射
    declare -A lib_to_pkg=(
        ["libgtk-3.so.0"]="libgtk-3-0"
        ["libXcomposite.so.1"]="libxcomposite1"
        ["libXdamage.so.1"]="libxdamage1"
        ["libXrandr.so.2"]="libxrandr2"
        ["libxkbcommon.so.0"]="libxkbcommon0"
        ["libcups.so.2"]="libcups2"
        ["libatk-bridge-2.0.so.0"]="libatk-bridge2.0-0"
        ["libatk-1.0.so.0"]="libatk1.0-0"
        ["libpango-1.0.so.0"]="libpango-1.0-0"
        ["libcairo.so.2"]="libcairo2"
        ["libgdk_pixbuf-2.0.so.0"]="libgdk-pixbuf2.0-0"
        ["libglib-2.0.so.0"]="libglib2.0-0"
        ["libgobject-2.0.so.0"]="libglib2.0-0"
        ["libXi.so.6"]="libxi6"
        ["libXfixes.so.3"]="libxfixes3"
        ["libXcursor.so.1"]="libxcursor1"
        ["libXinerama.so.1"]="libxinerama1"
        ["libXrender.so.1"]="libxrender1"
        ["libXtst.so.6"]="libxtst6"
        ["libnss3.so"]="libnss3"
        ["libasound.so.2"]="libasound2"
        ["libgbm.so.1"]="libgbm1"
        ["libdrm.so.2"]="libdrm2"
    )
    
    # 将缺失的库转换为包名
    for lib in $missing_libs; do
        pkg="${lib_to_pkg[$lib]}"
        if [ -n "$pkg" ] && [[ ! " $packages_to_install " =~ " $pkg " ]]; then
            packages_to_install="$packages_to_install $pkg"
        else
            # 尝试通用规则推测
            guessed_pkg=$(echo "$lib" | sed 's/\.so\.[0-9]*//' | sed 's/^lib//')
            if [ -n "$guessed_pkg" ] && [[ ! " $packages_to_install " =~ " lib$guessed_pkg " ]]; then
                packages_to_install="$packages_to_install lib$guessed_pkg"
            fi
        fi
    done
    
    if [ -n "$packages_to_install" ]; then
        echo -e "${CYAN}需要安装的包: $packages_to_install${NC}"
        
        # 显示安装进度
        show_process_status "正在下载和安装依赖包..."
        local install_pid=$(sudo apt-get install -y --no-install-recommends $packages_to_install 2>&1 >/dev/null & echo $!)
        
        local dots=""
        while ps -p $install_pid > /dev/null 2>&1; do
            dots="$dots."
            if [ ${#dots} -gt 3 ]; then
                dots="."
            fi
            echo -ne "${CYAN}安装依赖包中$dots${NC}\r"
            sleep 1
        done
        echo -ne "${GREEN}安装依赖包... 完成${NC}\n"
    fi
}

# RPM专用安装函数
function _install_deps_rpm() {
    local missing_libs="$1"
    show_process_status "RPM系统依赖安装功能待完善"
    log "请手动安装对应RPM包"
}

# 检查风云插件
function check_fengyun_plugin() {
    if [ -d "$FENGYUN_PLUGIN_PATH" ]; then
        FENGYUN_INSTALLED=true
        log "检测到风云插件"
    else
        FENGYUN_INSTALLED=false
    fi
}

# 检查现有安装
function check_existing_installation() {
    show_section "安装状态检查"
    
    local napcat_installed=false
    local qq_installed=false
    local fynt_installed=false
    
    # 检查各个组件
    [ -d "$TARGET_FOLDER/napcat" ] && napcat_installed=true
    [ -f "$QQ_EXECUTABLE" ] && qq_installed=true
    [ -f "/usr/local/bin/fynt" ] && fynt_installed=true
    check_fengyun_plugin
    
    # 显示检测结果
    echo -e "${CYAN}已安装的组件:${NC}"
    $napcat_installed && echo -e "  ${GREEN}✓ NapCat${NC}"
    $qq_installed && echo -e "  ${GREEN}✓ QQ${NC}"
    $fynt_installed && echo -e "  ${GREEN}✓ fynt启动器${NC}"
    $FENGYUN_INSTALLED && echo -e "  ${PURPLE}✨ 风云插件${NC}"
    
    echo ""
    
    # 如果什么都没安装，直接返回
    if ! $napcat_installed && ! $qq_installed && ! $fynt_installed && ! $FENGYUN_INSTALLED; then
        log "全新安装模式"
        return
    fi
    
    # 显示操作菜单
    echo -e "${YELLOW}请选择操作:${NC}"
    echo -e "  ${GREEN}1${NC}. 删除重装 QQ/NapCat/fynt (保留风云插件)"
    echo -e "  ${GREEN}2${NC}. 只更新 NapCat 和 QQ"
    echo -e "  ${GREEN}3${NC}. 只更新 fynt 启动器"
    echo -e "  ${GREEN}4${NC}. 安装/重装风云插件"
    echo -e "  ${GREEN}5${NC}. 退出安装"
    echo ""
    
    read -p "输入选项 [1-5]: " choice
    
    case $choice in
        1)
            log "删除重装 QQ/NapCat/fynt"
            echo -e "${YELLOW}注意: 风云插件会被保留${NC}"
            read -p "确认? (y/N): " confirm
            if [[ "$confirm" =~ ^[Yy]$ ]]; then
                rm -rf "$INSTALL_BASE_DIR" 2>/dev/null
                sudo rm -f /usr/local/bin/fynt 2>/dev/null
                INSTALL_QQ_NAPCAT=true
                INSTALL_FYNT=true
            else
                exit 0
            fi
            ;;
        2)
            log "只更新 NapCat 和 QQ"
            rm -rf "$INSTALL_BASE_DIR" 2>/dev/null
            INSTALL_QQ_NAPCAT=true
            INSTALL_FYNT=false
            ;;
        3)
            log "只更新 fynt 启动器"
            sudo rm -f /usr/local/bin/fynt 2>/dev/null
            INSTALL_QQ_NAPCAT=false
            INSTALL_FYNT=true
            ;;
        4)
            log "安装/重装风云插件"
            INSTALL_QQ_NAPCAT=false
            INSTALL_FYNT=false
            INSTALL_FENGYUN_ONLY=true
            ;;
        5)
            log "退出安装"
            exit 0
            ;;
        *)
            log "无效选项，使用默认安装"
            ;;
    esac
}

# 智能下载函数
function smart_download() {
    local url="$1"
    local output="$2"
    local name="$3"
    
    show_process_status "下载 $name..."
    
    # 先尝试 aria2
    if command -v aria2c &>/dev/null; then
        echo -e "${CYAN}使用aria2多线程下载...${NC}"
        aria2c -x 16 -s 16 -k 1M -o "$output" "$url" 2>&1 | while read line; do
            if [[ $line == \[DOWNLOAD\]* ]] && [[ $line == *\% ]]; then
                echo -ne "${CYAN}下载进度: $line${NC}\r"
            fi
        done
        
        if [ $? -eq 0 ] && [ -f "$output" ]; then
            echo -ne "${GREEN}下载 $name... 完成 (aria2)${NC}\n"
            return 0
        fi
    fi
    
    # 降级到 curl
    echo -e "${CYAN}使用curl下载...${NC}"
    curl -L -o "$output" "$url" 2>&1 | while read line; do
        if [[ $line == *\ \%* ]] && [[ $line == *.* ]]; then
            echo -ne "${CYAN}下载进度: $line${NC}\r"
        fi
    done
    
    if [ $? -eq 0 ] && [ -f "$output" ]; then
        echo -ne "${GREEN}下载 $name... 完成 (curl)${NC}\n"
        return 0
    fi
    
    # 最后尝试 wget
    echo -e "${CYAN}使用wget下载...${NC}"
    wget -O "$output" "$url" 2>&1 | while read line; do
        if [[ $line == *\%* ]] && [[ $line == *.* ]]; then
            echo -ne "${CYAN}下载进度: $line${NC}\r"
        fi
    done
    
    if [ $? -eq 0 ] && [ -f "$output" ]; then
        echo -ne "${GREEN}下载 $name... 完成 (wget)${NC}\n"
        return 0
    fi
    
    log "$name 下载失败"
    return 1
}

# 下载 NapCat
function download_napcat() {
    show_process_status "获取 NapCat..."
    
    local mirrors=(
        "https://github.com/NapNeko/NapCatQQ/releases/latest/download/NapCat.Shell.zip"
        "https://ghproxy.com/https://github.com/NapNeko/NapCatQQ/releases/latest/download/NapCat.Shell.zip"
    )
    
    for mirror in "${mirrors[@]}"; do
        if smart_download "$mirror" "NapCat.Shell.zip" "NapCat"; then
            return 0
        fi
    done
    
    return 1
}

# 安装 QQ
function install_qq() {
    show_section "安装 QQ"
    
    get_system_arch
    detect_package_manager
    
    local QQ_VERSION="3.2.20-40990"
    local qq_file=""
    
    # 确定下载文件
    if [ "$system_arch" = "amd64" ]; then
        if [ "$package_manager" = "apt-get" ]; then
            qq_file="QQ.deb"
            smart_download \
                "https://dldir1.qq.com/qqfile/qq/QQNT/ec800879/linuxqq_${QQ_VERSION}_amd64.deb" \
                "$qq_file" "QQ"
        else
            qq_file="QQ.rpm"
            smart_download \
                "https://dldir1.qq.com/qqfile/qq/QQNT/ec800879/linuxqq_${QQ_VERSION}_x86_64.rpm" \
                "$qq_file" "QQ"
        fi
    else
        if [ "$package_manager" = "apt-get" ]; then
            qq_file="QQ.deb"
            smart_download \
                "https://dldir1.qq.com/qqfile/qq/QQNT/ec800879/linuxqq_${QQ_VERSION}_arm64.deb" \
                "$qq_file" "QQ"
        else
            qq_file="QQ.rpm"
            smart_download \
                "https://dldir1.qq.com/qqfile/qq/QQNT/ec800879/linuxqq_${QQ_VERSION}_aarch64.rpm" \
                "$qq_file" "QQ"
        fi
    fi
    
    # 解压安装
    show_process_status "解压安装QQ..."
    mkdir -p "$INSTALL_BASE_DIR"
    
    if [ -f "$qq_file" ]; then
        if [[ "$qq_file" == *.deb ]]; then
            dpkg -x "$qq_file" "$INSTALL_BASE_DIR" 2>&1 | while read line; do
                echo -ne "${CYAN}解压安装... $line${NC}\r"
            done
        else
            rpm2cpio "$qq_file" | (cd "$INSTALL_BASE_DIR" && cpio -idmv 2>&1) | while read line; do
                echo -ne "${CYAN}解压安装... $line${NC}\r"
            done
        fi
        
        chmod +x "$QQ_EXECUTABLE" 2>/dev/null
        echo -ne "${GREEN}解压安装QQ... 完成${NC}\n"
        log "✓ QQ 安装完成"
        
        # 检查并安装缺失的依赖库
        check_and_install_deps
    fi
}

# 安装 NapCat 文件
function install_napcat_files() {
    show_process_status "安装 NapCat 插件..."
    
    if [ -f "NapCat.Shell.zip" ]; then
        unzip -q -o NapCat.Shell.zip -d ./NapCat 2>&1 | while read line; do
            echo -ne "${CYAN}解压NapCat... $line${NC}\r"
        done
        
        mkdir -p "$TARGET_FOLDER/napcat"
        cp -r ./NapCat/* "$TARGET_FOLDER/napcat/"
        chmod -R +x "$TARGET_FOLDER/napcat/"
        
        # 创建加载脚本
        cat > "$QQ_BASE_PATH/resources/app/loadNapCat.js" << 'EOF'
(async () => {
    await import('file://' + __dirname + '/app_launcher/napcat/napcat.mjs');
})();
EOF
        
        # 更新配置
        if command -v jq &>/dev/null; then
            jq '.main = "./loadNapCat.js"' "$QQ_BASE_PATH/resources/app/package.json" > /tmp/pkg.json
            mv /tmp/pkg.json "$QQ_BASE_PATH/resources/app/package.json"
        fi
        
        echo -ne "${GREEN}安装NapCat插件... 完成${NC}\n"
        log "✓ NapCat 配置完成"
    fi
}

# 安装 fynt 启动器
function install_fynt() {
    show_process_status "安装启动器..."
    
    curl -sL https://raw.gitcode.com/fengyunnb_admin/fengyun-plugin/raw/main/fynt -o /tmp/fynt
    if [ -f /tmp/fynt ]; then
        sudo mv /tmp/fynt /usr/local/bin/fynt
        sudo chmod +x /usr/local/bin/fynt
        log "✓ fynt 启动器安装完成"
    fi
}

# 安装风云插件
function install_fengyun() {
    show_section "安装风云插件"
    
    if $FENGYUN_INSTALLED && [ "$INSTALL_FENGYUN_ONLY" != "true" ]; then
        log "风云插件已安装，跳过"
        return
    fi
    
    echo -e "${PURPLE}正在安装风云插件...${NC}"
    bash <(curl -sL https://raw.gitcode.com/fengyunnb_admin/fengyun-plugin/raw/main/install.sh)
    
    if [ $? -eq 0 ]; then
        FENGYUN_INSTALLED=true
        log "✓ 风云插件安装完成"
    else
        log "风云插件安装失败"
    fi
}

# 清理文件
function clean_files() {
    show_process_status "清理临时文件..."
    
    rm -rf ./NapCat 2>/dev/null
    rm -f ./NapCat.Shell.zip 2>/dev/null
    rm -f ./QQ.deb 2>/dev/null
    rm -f ./QQ.rpm 2>/dev/null
    
    log "✓ 清理完成"
}

# 显示完成界面
function show_completion() {
    show_section "安装完成"
    
    echo -e "${GREEN}所有组件安装完成!${NC}"
    echo ""
    echo -e "${CYAN}安装位置:${NC}"
    echo -e "  QQ:        ${YELLOW}$QQ_EXECUTABLE${NC}"
    echo -e "  NapCat:    ${YELLOW}$TARGET_FOLDER/napcat${NC}"
    echo ""
    
    if $FENGYUN_INSTALLED; then
        echo -e "${PURPLE}✨ 风云插件已安装${NC}"
        echo -e "  路径: ${YELLOW}$FENGYUN_PLUGIN_PATH${NC}"
        echo ""
    fi
    
    echo -e "${CYAN}启动方式:${NC}"
    echo -e "  1. ${YELLOW}fynt${NC} (推荐)"
    echo -e "  2. ${YELLOW}xvfb-run -a $QQ_EXECUTABLE --no-sandbox${NC}"
    echo ""
    echo -e "${GREEN}══════════════════════════════════════════════════${NC}"
    
    # 如果没装风云插件，提示
    if ! $FENGYUN_INSTALLED && [ "$INSTALL_FENGYUN_ONLY" != "true" ]; then
        echo -e "${YELLOW}💡 提示: 如需风云插件可运行选项4安装${NC}"
    fi
    
    echo -e "${GREEN}感谢使用风云 NapCat 安装脚本!${NC}"
}

# 主函数
function main() {
    show_header
    check_sudo
    check_existing_installation
    
    if $INSTALL_QQ_NAPCAT; then
        install_dependency
        download_napcat
        install_qq
        install_napcat_files
    fi
    
    if $INSTALL_FYNT; then
        install_fynt
    fi
    
    if $INSTALL_FENGYUN_ONLY; then
        install_fengyun
    fi
    
    clean_files
    show_completion
}

# 运行主函数
main